<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入模板名称" :clearable="true" />
      </el-form-item>
      <el-form-item label="模板主题" prop="subject">
        <el-input v-model="queryParams.subject" placeholder="请输入模板主题" :clearable="true" />
      </el-form-item>
      <el-form-item label="模板类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请输入模板类型" :clearable="true">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MARKET_TEMPLATE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="MarketTemplate"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item label="修改" @click="handleMenuUpdate(row)" />
        <context-menu-item label="复制" @click="handleMenuCopy(row)" />
        <context-menu-item label="删除" @click="handleMenuDelete(row)" />
      </template>
    </Table>
  </ContentWrap>
</template>
<script setup name="MarketTemplate" lang="ts">
import { formatDate } from '@/utils/formatTime'
import { DICT_TYPE, getIntDictOptions, getDictObj } from '@/utils/dict'
import * as MarketTemplateApi from '@/api/market/marketTemplate'
import * as UserApi from '@/api/system/user/user'
const userList = ref()
const { push } = useRouter() // 路由
const message = useMessage() // 消息弹窗
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  subject: undefined,
  type: undefined
})
const queryData = ref()
const columns = [
  {
    label: '模板名称',
    prop: 'name'
  },
  {
    label: '模板主题',
    prop: 'subject'
  },
  {
    label: '模板类型',
    prop: 'type',
    formatter: ({ type }) => getDictObj(DICT_TYPE.MARKET_TEMPLATE_TYPE, type).label
  },
  {
    label: '创建人',
    prop: 'creatorId',
    formatter: ({ creatorId }) => userList.value.find((user) => user.id === creatorId)?.nickname
  },
  {
    label: '创建时间',
    prop: 'createTime',
    formatter: ({ createTime }) => formatDate(createTime)
  },
  {
    label: '更新时间',
    prop: 'updateTime',
    formatter: ({ updateTime }) => formatDate(updateTime)
  }
]
/** 初始化 */
onMounted(async () => {
  // 获得用户列表
  userList.value = await UserApi.listAllSimple()
  handleClickReset()
})

onActivated(() => {
  getPage()
})
/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  push({ name: 'MarketTemplateEdit', state: { id: 0, type: 'create' } })
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await MarketTemplateApi.pageQuery(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  push({ name: 'MarketTemplateEdit', state: { id: row.id, type: 'update' } })
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  push({ name: 'MarketTemplateEdit', state: { id: row.id, type: 'update' } })
}

/** 处理右击事件 复制 */
const handleMenuCopy = (row) => {
  push({ name: 'MarketTemplateEdit', state: { id: row.id, type: 'copy' } })
}

/** 处理右击事件 删除 */
const handleMenuDelete = async (row) => {
  await MarketTemplateApi.deleted(row.id)
  getPage()
  message.success('删除成功')
}
</script>
